package com.xl.rentkeeper.util;

import com.xl.rentkeeper.log.QLog;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.zip.Deflater;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * Created by zwc on 2015/6/24.
 */
public class ZipUtil {
    public static final String tag = "ZipUtil";
    public static byte[] gzip(byte[] in) {
        if (in == null || in.length == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            GZIPOutputStream gzip = new GZIPOutputStream(out) {
                {
                    this.def.setLevel(Deflater.BEST_COMPRESSION);
                }
            };
            gzip.write(in);
            gzip.close();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return out.toByteArray();
    }

    public static byte[] ungzip(byte[] in,int offset, int length) {
        ByteArrayInputStream bais = new ByteArrayInputStream(in,offset,length);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            GZIPInputStream gis = new GZIPInputStream(bais);
            int count;
            int BUFFER = 4096;
            byte[] data = new byte[BUFFER];
            while ((count = gis.read(data, 0, BUFFER)) != -1) {
                baos.write(data, 0, count);
            }
            byte[] retData = baos.toByteArray();
            baos.close();
            bais.close();
            gis.close();
            return retData;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static void zipFiles(ArrayList<File> logFiles, String zipFileName) {
        try {
            FileOutputStream outStream = new FileOutputStream(new File(zipFileName));
            ZipOutputStream zipStream = new ZipOutputStream(outStream);
            try {
                boolean writed = false ;
                for (File sourceFile : logFiles) {
                    if (sourceFile.exists()) {
                        zipStream.putNextEntry(new ZipEntry(sourceFile.getName()));
                        zipStream.setLevel(9);
                        long needReadLen = sourceFile.length();
                        FileInputStream in = new FileInputStream(sourceFile);
                        try {
                            byte[] buffer = new byte[20480];
                            int len = -1;
                            long readedLen = 0;
                            while ((len = in.read(buffer, 0, 20480)) != -1) {
                                zipStream.write(buffer, 0, len); // 耗时操作
                                writed = true;
                                readedLen = readedLen + len;
                                if (readedLen >= needReadLen) {
                                    break;
                                }
                            }
                        } finally {
                            in.close();
                            zipStream.flush();
                            zipStream.closeEntry();
                        }
                    }
                }
                if (writed) {
                    zipStream.close();
                }
                QLog.d(tag," zip stream close.");
            } catch (IOException e) {
                e.printStackTrace();
                QLog.d(tag," zip file error "+e,e);
            }finally{
                outStream.close();
                QLog.d(tag," file out stream close.");
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        QLog.d(tag, "zip file finish");
    }
}
